package com.haoxi.gateway.cmpp30msg;

import com.haoxi.gateway.util.TypeConvert;

public class CMPPCommon {
    public CMPPCommon () {
    }

    public static int ByteToInt ( byte mybyte ) {
        return mybyte;
    }

    public static byte IntToByte ( int i ) {
        return ( byte ) i;
    }

    public static int BytesToInt ( byte mybytes[] ) {
        return ( ( 0xff & mybytes[ 0 ] ) << 8 ) + mybytes[ 1 ];
    }

    public static byte[] IntToBytes ( int i ) {
        byte mybytes[] = new byte[2];
        mybytes[ 1 ] = ( byte ) ( 0xff & i );
        mybytes[ 0 ] = ( byte ) ( ( 0xff00 & i ) >> 8 );
        return mybytes;
    }

    public static byte[] IntToBytes4 ( int i ) {
        /*byte mybytes[] = new byte[4];
        mybytes[3] = (byte) (0xff & i);
        mybytes[2] = (byte) ( (0xff00 & i) >> 8);
        mybytes[1] = (byte) ( (0xff0000 & i) >> 16);
        mybytes[0] = (byte) ( (0xff000000 & i) >> 24);
        return mybytes;*/
        byte abyte0[] = new byte[4];
        abyte0[ 3 ] = ( byte ) i;
        i >>>= 8;
        abyte0[ 2 ] = ( byte ) i;
        i >>>= 8;
        abyte0[ 1 ] = ( byte ) i;
        i >>>= 8;
        abyte0[ 0 ] = ( byte ) i;
        return abyte0;
    }

    public static byte[] IntToBytes3 ( int i ) {
        byte mybytes[] = new byte[3];
        mybytes[ 2 ] = ( byte ) ( 0xff & i );
        mybytes[ 1 ] = ( byte ) ( ( 0xff00 & i ) >> 8 );
        mybytes[ 0 ] = ( byte ) ( ( 0xff0000 & i ) >> 16 );
        return mybytes;
    }

    public static void IntToBytes4 ( int i, byte mybytes[] ) {
        mybytes[ 3 ] = ( byte ) ( 0xff & i );
        mybytes[ 2 ] = ( byte ) ( ( 0xff00 & i ) >> 8 );
        mybytes[ 1 ] = ( byte ) ( ( 0xff0000 & i ) >> 16 );
        mybytes[ 0 ] = ( byte ) ( int ) ( ( ( long ) 0xff000000 & ( long ) i ) >> 24 );
    }

    public static long BytesToLong ( byte mybytes[] ) {
        return ( ( long ) 255 & ( long ) mybytes[ 0 ] ) << 24 | ( ( long ) 255 & ( long ) mybytes[ 1 ] ) << 16 |
               ( ( long ) 255 & ( long ) mybytes[ 2 ] ) << 8 | ( long ) mybytes[ 3 ];
    }

    public static int Bytes4ToInt ( byte mybytes[] ) {
        return ( 0xff & mybytes[ 0 ] ) << 24 | ( 0xff & mybytes[ 1 ] ) << 16 | ( 0xff & mybytes[ 2 ] ) << 8 | 0xff & mybytes[ 3 ];
    }

    public static byte[] LongToBytes ( long l ) {
        byte mybytes[] = new byte[4];
        mybytes[ 3 ] = ( byte ) ( int ) ( ( long ) 255 & l );
        mybytes[ 2 ] = ( byte ) ( int ) ( ( ( long ) 65280 & l ) >> 8 );
        mybytes[ 1 ] = ( byte ) ( int ) ( ( ( long ) 0xff0000 & l ) >> 16 );
        mybytes[ 0 ] = ( byte ) ( int ) ( ( ( long ) 0xff000000 & l ) >> 24 );
        return mybytes;
    }

    public static int getRandomInt () {
        Double doub = new Double ( Math.random () * ( double ) - 1 );
        return doub.intValue ();
    }

    public static void BytesCopy ( byte source[], byte dest[], int sourcebegin, int sourceend, int destbegin ) {
        int j = 0;
        for ( int i = sourcebegin; i <= sourceend; i++ ) {
            dest[ destbegin + j ] = source[ i ];
            j++;
        }
    }
    public static String getTimeStamp()
    {
        java.text.SimpleDateFormat format = new java.text.SimpleDateFormat("MMddHHmmss");
        return format.format(java.util.Calendar.getInstance().getTime());
    }

    public static final int MAX_QUE_LENGTH = 100;
    public static final int MAX_MSG_CONTENT_LENGTH = 140;
    public static final int MSG_HEAD_LENGTH = 12;
    public static final int MSG_CONNECT_BODY_LENGTH = 27;
    public static final int MSG_CONNECT_REP_BODY_LENGTH = 21;
    public static final int MSG_SUBMIT_REP_BODY_LENGTH = 12;
    public static final int MSG_QUERY_BODY_LENGTH = 27;
    public static final int MSG_QUERY_REP_BODY_LENGTH = 51;
    public static final int MSG_STATUS_REPORT_LENGTH = 60;
    public static final int MSG_DELIVER_REP_BODY_LENGTH = 12;
    public static final int MSG_CANCEL_BODY_LENGTH = 8;
    public static final int MSG_CANCEL_REP_BODY_LENGTH = 1;
    public static final int MSG_ACTIVETEST_BODY_LENGTH = 0;
    public static final int MSG_ACTIVETEST_REP_BODY_LENGTH = 1;
    public static final byte RESERVED = 0;
    public static final int SUBMIT_OK = 0;
    public static final int SUBMIT_ERROR_STRUCTURE = 1;
    public static final int SUBMIT_ERROR_COMMANDTYPE = 2;
    public static final int SUBMIT_ERROR_SEQ_DUPLICATE = 3;
    public static final int SUBMIT_ERROR_MSG_LENGTH = 4;
    public static final int SUBMIT_ERROR_FEECODE = 5;
    public static final int SUBMIT_ERROR_CONTENT_LENGTH_EXCEED = 6;
    public static final int SUBMIT_ERROR_SERVERID = 7;
    public static final int SUBMIT_ERROR_FLOW_CONTROL = 8;
    public static final int SUBMIT_ERROR_OTHERS = 9;
    public static final int CMPP_CONNECT = 1;
    public static final int CMPP_CONNECT_REP = 0x80000001;
    public static final int CMPP_TERMINATE = 2;
    public static final int CMPP_TERMINATE_REP = 0x80000002;
    public static final int CMPP_SUBMIT = 4;
    public static final int CMPP_SUBMIT_REP = 0x80000004;
    public static final int CMPP_DELIVER = 5;
    public static final int CMPP_DELIVER_REP = 0x80000005;
    public static final int CMPP_QUERY = 6;
    public static final int CMPP_QUERY_REP = 0x80000006;
    public static final int CMPP_CANCEL = 7;
    public static final int CMPP_CANCEL_REP = 0x80000007;
    public static final int CMPP_ACTIVE_TEST = 8;
    public static final int CMPP_ACTIVE_TEST_REP = 0x80000008;
    public static final byte SUBMIT_DATA = 1;
    public static final byte END_SMS = 2;
    public static final byte SMSGW_RESP = 3;
    public static final int MSG_TYPE_ASCII = 0;
    public static final int MSG_TYPE_WRITECARD = 3;
    public static final int MSG_TYPE_BINARY = 4;
    public static final int MSG_TYPE_EMS = 5;
    public static final int MSG_TYPE_UCS2 = 8;
    public static final int MSG_TYPE_CHINESE = 15;
    
    
    public static void main(String args[]){
        
        int num = 0;
//        byte[] result = IntToBytes4(num);
        byte[] result = TypeConvert.int2byte(num);
        String sResult = "";
        for (int i=0; i < result.length; ++i){
            System.out.println("the " + i + " th" + result[i]);
            sResult = sResult + result[i];
        }
        
        System.out.println("the end result : " + sResult);
        byte[] result1 = new byte[4];
        for (int i=0; i < result1.length; ++i){
            System.out.println("the " + i + " th" + result1[i]);
            sResult = sResult + result1[i];
        }
        
        System.out.println("the end result : " + sResult);
    }

}